unit uPesquisaInteressados;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, dxCore, dxButton, JvExStdCtrls, JvCombobox,
  JvDBSearchComboBox, Mask, JvExMask, JvSpin, JvDBSpinEdit, XPMenu, Grids,
  DBGrids, JvExDBGrids, JvDBGrid, ExtCtrls, MMJPanel, DB, FMTBcd, SqlExpr,
  DBClient, Provider, RXSpin;

type
  TfPesquisaInteressados = class(TForm)
    MMJPanel1: TMMJPanel;
    MMJPanel2: TMMJPanel;
    JvDBGrid1: TJvDBGrid;
    XPMenu1: TXPMenu;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Panel1: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label5: TLabel;
    cbimovel: TComboBox;
    Label6: TLabel;
    Label7: TLabel;
    cbTipo: TComboBox;
    dxButton2: TdxButton;
    dxButton3: TdxButton;
    dxButton4: TdxButton;
    dxButton5: TdxButton;
    dxButton1: TdxButton;
    Label8: TLabel;
    valorDe: TEdit;
    valorAte: TEdit;
    Label13: TLabel;
    dsp: TDataSetProvider;
    cds: TClientDataSet;
    DtSrc: TDataSource;
    sds: TSQLDataSet;
    sdsNOME: TStringField;
    sdsTEL1: TStringField;
    sdsTEL2: TStringField;
    cdsNOME: TStringField;
    cdsTEL1: TStringField;
    cdsTEL2: TStringField;
    q1: TRxSpinEdit;
    q2: TRxSpinEdit;
    s1: TRxSpinEdit;
    s2: TRxSpinEdit;
    c1: TRxSpinEdit;
    c2: TRxSpinEdit;
    g2: TRxSpinEdit;
    g1: TRxSpinEdit;
    sdsCOD_INTERESSADO: TIntegerField;
    sdsCOD_ENDERECO: TIntegerField;
    sdsNR: TStringField;
    sdsCOMPLEMENTO: TStringField;
    sdsIMOVEL: TStringField;
    sdsIMOVELDE: TStringField;
    sdsTIPO: TStringField;
    sdsBAIRROIMOVEL: TStringField;
    sdsVALOR: TFloatField;
    sdsN_QUARTOS: TStringField;
    sdsN_SALAS: TStringField;
    sdsN_COZINHAS: TStringField;
    sdsN_GARAGENS: TStringField;
    sdsATENDIDO: TStringField;
    sdsCPF: TStringField;
    sdsRG: TStringField;
    sdsOBS: TStringField;
    sdsEMAIL: TStringField;
    sdsDATA_CADASTRO: TDateField;
    sdsCIDADEIMOVEL: TStringField;
    sdsRUAIMOVEL: TStringField;
    cdsCOD_INTERESSADO: TIntegerField;
    cdsCOD_ENDERECO: TIntegerField;
    cdsNR: TStringField;
    cdsCOMPLEMENTO: TStringField;
    cdsIMOVEL: TStringField;
    cdsIMOVELDE: TStringField;
    cdsTIPO: TStringField;
    cdsBAIRROIMOVEL: TStringField;
    cdsVALOR: TFloatField;
    cdsN_QUARTOS: TStringField;
    cdsN_SALAS: TStringField;
    cdsN_COZINHAS: TStringField;
    cdsN_GARAGENS: TStringField;
    cdsATENDIDO: TStringField;
    cdsCPF: TStringField;
    cdsRG: TStringField;
    cdsOBS: TStringField;
    cdsEMAIL: TStringField;
    cdsDATA_CADASTRO: TDateField;
    cdsCIDADEIMOVEL: TStringField;
    cdsRUAIMOVEL: TStringField;
    cbCidade: TComboBox;
    cbBairro: TComboBox;
    cbRua: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure dxButton2Click(Sender: TObject);
    procedure dxButton5Click(Sender: TObject);
    procedure dxButton1Click(Sender: TObject);
    procedure dxButton3Click(Sender: TObject);
    procedure JvDBGrid1CellClick(Column: TColumn);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fPesquisaInteressados: TfPesquisaInteressados;

implementation

uses UDM, uProcuraImovel, uInteressado, uPesquisa_Locacao;

{$R *.dfm}

procedure TfPesquisaInteressados.FormCreate(Sender: TObject);
var
  ScreenHeight: LongInt;
  ScreenWidth: LongInt;
begin
  // tamanho do form
  ScreenWidth := 800;
  ScreenHeight := 600;
  if (screen.width <> ScreenWidth) then
  begin
    height := longint(height) * longint(screen.height) DIV ScreenHeight;
    width := longint(width) * longint(screen.width) DIV ScreenWidth;
    scaleBy(screen.width, ScreenWidth);
  end;
end;

procedure TfPesquisaInteressados.FormShow(Sender: TObject);
begin
  if (dm.cds_cep.Active) then
      dm.cds_cep.Close;
  dm.cds_cep.Params[0].Clear;
  dm.cds_cep.Params[1].AsInteger := 9999999;
  dm.cds_cep.Open;
  dm.cds_cep.First;
  while not dm.cds_cep.Eof do
  begin
     cbRua.Items.Add(dm.cds_cepRUA.AsString);
     dm.cds_cep.Next;
  end;
  dm.cds_cep.Close;

  if (not dm.cds_bairro.Active) then
      dm.cds_bairro.Open;
  dm.cds_bairro.First;
  while not dm.cds_bairro.Eof do
  begin
     cbBairro.Items.Add(dm.cds_bairroBAIRRO.AsString);
     dm.cds_bairro.Next;
  end;
  dm.cds_bairro.Close;

  if (not dm.cds_cidade.Active) then
      dm.cds_cidade.Open;
  dm.cds_cidade.First;
  while not dm.cds_cidade.Eof do
  begin
     cbCidade.Items.Add(dm.cds_cidadeCIDADE.AsString);
     dm.cds_cidade.Next;
  end;
  dm.cds_cidade.Close;

end;

procedure TfPesquisaInteressados.dxButton2Click(Sender: TObject);
var
    sqltexto, sqltexto1 : string;
begin
   if (cds.Active) then
       cds.Close;
   cds.CommandText := '';
   sqltexto := '';
   sqltexto1 := '';
   sqltexto := 'select * from INTERESSADO ';

   if (cbRua.Text <> '') then
     sqltexto1 := ' where RUAIMOVEL = ' + QuotedStr(cbRua.Text);

   if (cbBairro.Text <> '') then
   if (sqltexto1 = '') then
     sqltexto1 := ' where BAIRROIMOVEL = ' + QuotedStr(cbBairro.Text)
   else
     sqltexto1 := sqltexto1 + ' and BAIRROIMOVEL = ' + QuotedStr(cbBairro.Text);

   if (cbCidade.Text <> '') then
   if (sqltexto1 = '') then
     sqltexto1 := ' where CIDADEIMOVEL = ' + QuotedStr(cbCidade.Text)
   else
     sqltexto1 := sqltexto1 + ' and CIDADEIMOVEL = ' + QuotedStr(cbCidade.Text);

   if ((valorDe.Text <> '') and (valorAte.Text <> '')) then
      if (sqltexto1 = '') then
        sqltexto1 := ' where VALOR between ' + (valorDe.Text) + ' and ' + (valorAte.Text)
      else
        sqltexto1 := sqltexto1 + ' and VALOR between ' + (valorDe.Text) + ' and ' + (valorAte.Text);

   if (cbimovel.Text <> '') then
   if (sqltexto1 = '') then
     sqltexto1 := ' where IMOVEL = ' + QuotedStr(cbimovel.Text)
   else
     sqltexto1 := sqltexto1 + ' and IMOVEL = ' + QuotedStr(cbimovel.Text);

   if (cbTipo.Text <> '') then
   if (sqltexto1 = '') then
     sqltexto1 := ' where TIPO = ' + QuotedStr(cbTipo.Text)
   else
     sqltexto1 := sqltexto1 + ' and TIPO = ' + QuotedStr(cbTipo.Text);

   if ((q1.Text <> '0') and (q2.Text <> '0')) then
      if (sqltexto1 = '') then
        sqltexto1 := ' where N_QUARTOS between ' + QuotedStr(q1.Text) + ' and ' + QuotedStr(q2.Text)
      else
        sqltexto1 := sqltexto1 + ' and N_QUARTOS between ' + QuotedStr(q1.Text) + ' and ' + QuotedStr(q2.Text);

   if ((s1.Text <> '0') and (s2.Text <> '0')) then
      if (sqltexto1 = '') then
        sqltexto1 := ' where N_SALAS between ' + QuotedStr(s1.Text) + ' and ' + QuotedStr(s2.Text)
      else
        sqltexto1 := sqltexto1 + ' and N_SALAS between ' + QuotedStr(s1.Text) + ' and ' + QuotedStr(s2.Text);

   if ((c1.Text <> '0') and (c2.Text <> '0')) then
      if (sqltexto1 = '') then
        sqltexto1 := ' where N_COZINHAS between ' + QuotedStr(c1.Text) + ' and ' + QuotedStr(c2.Text)
      else
        sqltexto1 := sqltexto1 + ' and N_COZINHAS between ' + QuotedStr(c1.Text) + ' and ' + QuotedStr(c2.Text);

   if ((g1.Text <> '0') and (g2.Text <> '0')) then
      if (sqltexto1 = '') then
        sqltexto1 := ' where N_GARAGENS between ' + QuotedStr(g1.Text) + ' and ' + QuotedStr(g2.Text)
      else
        sqltexto1 := sqltexto1 + ' and N_GARAGENS between ' + QuotedStr(g1.Text) + ' and ' + QuotedStr(g2.Text);

   sqltexto := sqltexto + sqltexto1;

   cds.CommandText := sqltexto;
   cds.Open;

end;

procedure TfPesquisaInteressados.dxButton5Click(Sender: TObject);
begin
  close;
end;

procedure TfPesquisaInteressados.dxButton1Click(Sender: TObject);
begin
    fPesquisa_Locacao:=TfPesquisa_Locacao.Create(Application);
    try
     fPesquisa_Locacao.ShowModal;
    finally
     fPesquisa_Locacao.Free;
    end;
end;

procedure TfPesquisaInteressados.dxButton3Click(Sender: TObject);
begin
  cbRua.Text := '';
  cbBairro.Text := '';
  cbCidade.Text := '';
  cbimovel.Text := '';
  cbTipo.Text := '';
  valorDe.Text := '';
  valorAte.Text := '';
  q1.Text := '0';
  q2.Text := '0';
  s1.Text := '0';
  s2.Text := '0';
  c1.Text := '0';
  c2.Text := '0';
  g1.Text := '0';
  g2.Text := '0';  
end;

procedure TfPesquisaInteressados.JvDBGrid1CellClick(Column: TColumn);
begin
     fInteressado:=TfInteressado.Create(Application);
     try
        Dm.cds_interessado.Close;
        dm.cds_interessado.Params[0].Clear;
        Dm.cds_interessado.Params[0].AsInteger:=cdsCOD_INTERESSADO.AsInteger;
        Dm.cds_interessado.Open;
        fInteressado.cbRua1.Text := cdsRUAIMOVEL.AsString;
        fInteressado.cbBairro1.Text := cdsBAIRROIMOVEL.AsString;
        fInteressado.cbCidade1.Text := cdsCIDADEIMOVEL.AsString;
        fInteressado.ShowModal;
     finally
       fInteressado.Free;
     end;
end;

end.
